import { UserEmojiPackItemType, EmojiPackItemUpdateType } from "@/types/user-emoji";
import {create} from "zustand";
import loading from "@/components/Global/Loading";

interface UserEmojiState {
    state: {
        emojiItems: UserEmojiPackItemType[];
        loading: boolean;
    };
    actions: {
        setLoading: (loading: boolean) => void;
        setEmojiItems: (items: UserEmojiPackItemType[]) => void;
        addEmojiItem: (item: UserEmojiPackItemType) => void;
        deleteEmojiItem: (itemId: string) => void;
        deleteBatchEmojiItems: (itemIds: string[]) => void;
        updateEmojiItem: (update: EmojiPackItemUpdateType) => void;
        sortEmojiItems: () => void;
    };
}

const UserEmojiStore = create<UserEmojiState>((set, get) => ({
    state: {
        emojiItems: [],
        loading: false,
    },
    actions: {
        setLoading(loading: boolean) {
            set((current) => ({
                state: {
                    ...current.state,
                    loading,
                },
            }));
        },
        setEmojiItems(items: UserEmojiPackItemType[]) {
            set((current) => ({
                state: {
                    ...current.state,
                    emojiItems: items,
                },
            }));
        },
        addEmojiItem(item: UserEmojiPackItemType) {
            set((current) => ({
                state: {
                    ...current.state,
                    emojiItems: [...current.state.emojiItems, item],
                },
            }));
        },
        deleteEmojiItem(itemId: string) {
            set((current) => ({
                state: {
                    ...current.state,
                    emojiItems: current.state.emojiItems.filter(i => i.id !== itemId),
                },
            }));
        },
        deleteBatchEmojiItems(itemIds: string[]) {
            set((current) => ({
                state: {
                    ...current.state,
                    emojiItems: current.state.emojiItems.filter(i => !itemIds.includes(i.id)),
                },
            }));
        },
        updateEmojiItem(update: EmojiPackItemUpdateType) {
            set((current) => ({
                state: {
                    ...current.state,
                    emojiItems: current.state.emojiItems.map(i =>
                        i.id === update.id
                            ? { ...i, ...update }
                            : i
                    ),
                },
            }));
        },
        sortEmojiItems(): void {
            set((current) => {
                const sortedItems = [...current.state.emojiItems].sort((a, b) => {
                    return b.sortOrder - a.sortOrder || new Date(b.createTime).getTime() - new Date(a.createTime).getTime();
                });
                return {
                    state: {
                        ...current.state,
                        emojiItems: sortedItems,
                    },
                };
            });
        }

    },
}));

export default UserEmojiStore;